package cz.schovjan.proj.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import cz.schovjan.proj.dao.IBandDao;
import cz.schovjan.proj.dao.IMemberDao;
import cz.schovjan.proj.dao.ISongDao;
import cz.schovjan.proj.dao.IConcertDao;
import cz.schovjan.proj.dto.Band;
import cz.schovjan.proj.dto.Concert;
import cz.schovjan.proj.dto.Member;
import cz.schovjan.proj.dto.Song;
import cz.schovjan.proj.entity.BandEntity;
import cz.schovjan.proj.entity.ConcertEntity;
import cz.schovjan.proj.entity.MemberEntity;
import cz.schovjan.proj.entity.SongEntity;
import cz.schovjan.proj.service.IBand;

/**
 * Implementace rozhrani IBand pro praci s kapelami.
 * @author schovjan
 *
 */
@Transactional
public class BandImpl implements IBand {

	@Autowired
	private IBandDao bandDao;
	
	@Autowired
	private IMemberDao memberDao;
	
	@Autowired
	private ISongDao songDao;
	
	@Autowired
	private IConcertDao concertDao;
	
	
	@Override
	public List<Band> findAll() {
		List<Band> result = new ArrayList<Band>();
		List<BandEntity> bandEntitys = bandDao.findAll();
		
		for (BandEntity be : bandEntitys) {
			result.add(loadBand(be));
		}
		
		return result;
	}
	
	
	@Override
	public Band find(int id) {
		BandEntity be = bandDao.find(id);

		return loadBand(be);
	}
	
	
	/**
	 * Vrati nasetovanou kapelu.
	 * @param be
	 * @return
	 */
	private Band loadBand(BandEntity be) {
		
		if (be==null) {
			return null;
		}
		Band b = new Band();
		b.setId(be.getId());
		b.setName(be.getName());
		b.setDescription(be.getDescription());
		b.setEmail(be.getEmail());
		b.setCreation(be.getCreation());
		b.setBreakup(be.getBreakup());
		
		//clenove kapely
		List<Member> members = new ArrayList<Member>();
		Member m;
		for (MemberEntity me : be.getMembers()) {
			m = new Member();
			m.setId(me.getId());
			m.setNick(me.getNick());
			m.setName(me.getName());
			m.setSurname(me.getSurname());
			m.setEmail(me.getEmail());
			m.setPhone(me.getPhone());
			m.setJob(me.getJob());
			
			members.add(m);
		}
		b.setMembers(members);
		
		//repertoar
		List<Song> songs = new ArrayList<Song>();
		Song s;
		for (SongEntity se : be.getSongs()) {
			s = new Song();
			s.setId(se.getId());
			s.setNameCs(se.getNameCs());
			s.setNameOriginal(se.getNameOriginal());
			s.setArrange(se.getArrange());
			s.setAuthor(se.getAuthor());
			s.setDescription(se.getDescription());
			
			songs.add(s);
		}
		b.setSongs(songs);
		
		//koncerty
		List<Concert> concerts = new ArrayList<Concert>();
		Concert c;
		for (ConcertEntity ce : be.getConcerts()) {
			c = new Concert();
			c.setId(ce.getId());
			c.setPlace(ce.getPlace());
			c.setDescription(ce.getDescription());
			c.setDateTime(ce.getDateTime());
			
			concerts.add(c);
		}
		b.setConcerts(concerts);

		return b;
	}


	@Override
	public void add(String name, String email, String description, Date creation, Date breakup) {
		BandEntity be = new BandEntity();
		be.setBreakup(breakup);
		be.setCreation(creation);
		be.setDescription(description);
		be.setEmail(email);
		be.setName(name);
		
		bandDao.add(be);
	}
	
	
	@Override
	public void delete(int id) {
		bandDao.delete(id);
	}
	
	
	@Override
	public void save(Integer id, String name, String email, String description,	Date creation, Date breakup) {
		BandEntity be = bandDao.find(id);
		be.setName(name);
		be.setEmail(email);
		be.setDescription(description);
		be.setCreation(creation);
		be.setBreakup(breakup);
	}
	
	
	@Override
	public void addMember(Integer idB, Integer idM) {
		BandEntity be = bandDao.find(idB);
		MemberEntity me = memberDao.find(idM);
		be.getMembers().add(me);
	}
	
	
	@Override
	public void removeMember(Integer idB, Integer idM) {
		BandEntity be = bandDao.find(idB);
		MemberEntity me = memberDao.find(idM);
		be.getMembers().remove(me);
	}
	
	
	@Override
	public void addSong(Integer idB, Integer idS) {
		BandEntity be = bandDao.find(idB);
		SongEntity se = songDao.find(idS);
		be.getSongs().add(se);
	}
	
	
	@Override
	public void removeSong(Integer idB, Integer idS) {
		BandEntity be = bandDao.find(idB);
		SongEntity se = songDao.find(idS);
		be.getSongs().remove(se);
	}
	
	
	@Override
	public void addConcert(Integer idB, Integer idC) {
		BandEntity be = bandDao.find(idB);
		ConcertEntity ce = concertDao.find(idC);
		be.getConcerts().add(ce);
	}
	
	
	@Override
	public void removeConcert(Integer idB, Integer idC) {
		BandEntity be = bandDao.find(idB);
		ConcertEntity ce = concertDao.find(idC);
		be.getConcerts().remove(ce);
	}
	
}
